// timerd.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include <TCHAR.H>
#include <MALLOC.H>
#include <Shellapi.h>
#include <stdlib.h>

TCHAR msg[4096] = _T("Hello");

int tt = 0;
static DWORD WINAPI beep(LPVOID p )
{
    Beep(0x900,(DWORD)p);
    return 0;
}

static long count = 0;
int WINAPI CALLBACK func(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
  )
{
    HWND d = GetDlgItem(hwndDlg,IDC_T1);
    switch (uMsg)
    {
        case WM_INITDIALOG:
            {
                SetTimer(hwndDlg,1,1000,0);
            }
        case WM_TIMER:
            {
                TCHAR* p = (TCHAR*)_alloca(_tcslen(msg)* sizeof (msg[1]) + 256);
                SYSTEMTIME time;
                GetLocalTime (&time);
                _stprintf(p,_T("%d:%d:%d %s"),time.wHour,time.wMinute,time.wSecond,msg);
                SendMessage(d,WM_SETTEXT,0,(LONG)p);
                count ++;
                if ( count > 1800)
                {
                    EndDialog(hwndDlg, LOWORD(wParam));
                }
                else if (tt <=0 && count < 10)
                {
                    QueueUserWorkItem(beep,(LPVOID)100,0);
                }
                else if (tt > 0 && count >= tt)
                {

                    QueueUserWorkItem(beep,(LPVOID)100,0);
                    SetWindowPos(hwndDlg, HWND_TOPMOST, 0, 0, 0, 0, 
                 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
                }
            }
        case WM_COMMAND:
            if (LOWORD(wParam) == IDC_OK || LOWORD(wParam) == IDC_CANCEL || wParam == 0x2) 
            {
                EndDialog(hwndDlg, LOWORD(wParam));
                return TRUE;
        }
    }

	return FALSE;
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{
    int argc;
    LPWSTR* argv = CommandLineToArgvW(lpCmdLine, & argc);
    if (argc > 0 && argv[0][0] == '/')
    {
        LPWSTR time = & argv[0][1];
        tt = _wtoi(time);
    }
    _tcscpy(msg,lpCmdLine);
    int i = DialogBox(hInstance,MAKEINTRESOURCE(IDD_D1),0,func);
	return 0;
}



	